package com.bailiban.aigou.admin.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.bailiban.aigou.admin.common.BaseAction;
import com.bailiban.aigou.admin.entity.CarouselFigure;
import com.bailiban.aigou.admin.entity.Classification;
import com.bailiban.aigou.admin.entity.Product;
import com.bailiban.aigou.admin.entity.SearchHistory;
import com.bailiban.aigou.admin.service.ICarouselFigureService;
import com.bailiban.aigou.admin.service.ICommodityclassificationService;
import com.bailiban.aigou.admin.service.IProductService;
import com.bailiban.aigou.admin.service.ISearchHistoryService;

@Controller
@Scope("prototype")
@Namespace("/front")
public class FrontIndexAction extends BaseAction {

	/**
	 * 前台action localhost:8080/aigou/front/index
	 */
	private static final long serialVersionUID = 1L;
	
	/**
	 * 注入
	 */
	@Autowired
	 private ICarouselFigureService carouselFigureService;
	@Autowired
	private ICommodityclassificationService CommodityclassificationService;
	@Autowired
	private ISearchHistoryService SearchHistoryService;
	@Autowired
	private IProductService productService;
	
	/**
	 * 主页 需要index
	 * @return
	 */
	@Action(value="index",results={ @Result( name="success" ,location ="/WEB-INF/jsp/aigou/front/index/index.jsp")})
	public String index(){
		HttpServletRequest request = ServletActionContext.getRequest();
		//轮播图
		List<CarouselFigure> allcarouselFigures = carouselFigureService.getAllcarouselFigure();
		//分类
		List<Classification> allProductTypes = CommodityclassificationService.getAllProductTypes();
		//查询热搜词
		List<SearchHistory> searchHistorys = SearchHistoryService.getSearchHistoryPages(10);
		//新品
		List<Product> newProducts = productService.getNewProducts(6);
		//排行榜
		List<Product> rankings = productService.getProductRankings();
		//全球进口
		Classification productType = new Classification();
		productType.setName("全球进口");
		Product product = new Product();
		product.setClassification(productType);
		List<Product> list = productService.getProductsByType(product, 5);
		//服装服饰
		Classification productType2 = new Classification();
		Product product2 = new Product();
		productType2.setName("服装服饰");
		product2.setClassification(productType2);
		List<Product> list2 = productService.getProductsByType(product2, 12);
		//护肤美妆
		productType.setName("护肤美妆");
		product.setClassification(productType);
		List<Product> list3 = productService.getProductsByType(product, 5);
		//图书学习
		productType.setName("图书学习");
		product.setClassification(productType);
		List<Product> list4 = productService.getProductsByType(product, 12);
		
		
		request.setAttribute("allcarouselFigures", allcarouselFigures); //传allcarouselFigures
		request.setAttribute("allProductTypes", allProductTypes);//传allProductTypes
		request.setAttribute("searchHistorys", searchHistorys);//热搜词
		request.setAttribute("newProducts", newProducts);//新品
		request.setAttribute("rankings", rankings);//排行榜
		request.setAttribute("list", list);//全球进 口
		request.setAttribute("list2", list2);//服装服饰
		request.setAttribute("list3", list3);//护肤美妆
		request.setAttribute("list4", list4);//图书学习
		
		return SUCCESS;
	}

}
